<?php
require_once 'MyPdo.class.php';

/*
	 id
	 name
	 levels
	 parent
	 orders
*/
class zheng_zhuang{

	private $p = null;
	private $table = 'bianzheng_zhengzhuang';
	function __construct(){
		if(!$this->p){
			$this->p =  MyPdo::init();
		}
	}

	//删除症状
	function _delSubItem($id){
		$sql = "delete from {$this->table} where id = ?";
		return $this->p->dml($sql,array($id));
	}


	//症状排序的上下移动
	function _itemUpDown($id,$ctype){
		if($ctype == 'up'){
			$sql = "update {$this->table} set orders=orders+1 where id = ?";
		}else{
			$sql = "update {$this->table} set orders=if(orders-1<1,1,orders-1) where id = ?";
		}
		return $this->p->dml($sql,array($id));
	}


	//重命名分类名
	function _classRname($id,$name){
		$sqlCheck = "select id from {$this->table} where name = ?";
		$resCheck = $this->p->dql($sqlCheck,array($name),1);
		if($resCheck){
			return false;	//分类名存在 
		}
		$sql = "update {$this->table} set name = ? where id = ?";
		$res = $this->p->dml($sql,array($name,$id));
		return $res;
	}


	//添加症状
	function _addClassItem($classId,$name){
		$sql = "select id from {$this->table} where name = ?";
		$res = $this->p->dql($sql,array($name),1);
		if($res){
			return false;	//症状存在
		}
		$sqlMax = "select max(orders) max from {$this->table} where parent = ?";
		$resMax = $this->p->dql($sqlMax,array($classId),1);
		$max = !$resMax['max']?1:$resMax['max']+1;

		$sql2 = "insert into {$this->table}(name,levels,parent,orders) values(?,2,?,?)";
		$res2 = $this->p->dml($sql2,array($name,$classId,$max));
		return $res2;
	}


	//获取症状分类信息
	function _getClassList(){
		$sql = "select * from {$this->table} order by orders";
		return $this->p->dql($sql,array());
	}


	//添加症状分类
	function _addClass($typeName){
		$sqlCheck = "select id from {$this->table} where name = ?";
		$resCheck = $this->p->dql($sqlCheck,array($typeName),1);
		if($resCheck){
			return false;
		}

		//获取最大排序号
		$sqlMax = "select max(orders) max from {$this->table}";
		$resMax = $this->p->dql($sqlMax,array(),1);
		$max = !$resMax['max']?1:$resMax['max']+1;

		//添加分类名
		$sql = "insert into {$this->table} values(null,?,1,0,$max)";
		$res = $this->p->dml($sql,array($typeName));
		if($res){
			return true;
		}
		return false;
	}


}
